//
//  FQDPraiseOnMeCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/28/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDPraiseOnMeCell.h"
#import "NSDate+FQDExtension.h"
#import "FQDMyCommentMessageCellItem.h"
#import "FQDRestModel.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

static CGFloat const kAvatarImageSize = 35.0f;

@interface FQDPraiseOnMeCell ()

@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIView *separatorLine;
@property (nonatomic, strong) UILabel *targetContentLabel;

@end

@implementation FQDPraiseOnMeCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareCell];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareCell
{
    UIImage *defaultAvatarImage = [UIImage imageNamed:@"avatar_default_icon_normal"];
    _avatarImageView = [[UIImageView alloc] initWithImage:defaultAvatarImage];
    _avatarImageView.layer.cornerRadius = kAvatarImageSize / 2;
    _avatarImageView.clipsToBounds = YES;
    [self.contentView addSubview:_avatarImageView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = FQDRegularFontWithSize(14.0f);
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_nameLabel];
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.font = FQDRegularFontWithSize(12.0f);
    _dateLabel.textColor = RGB(179, 179, 179);
    _dateLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_dateLabel];
    
    _separatorLine = [[UIView alloc] init];
    _separatorLine.backgroundColor = RGB(237, 237, 237);
    [self.contentView addSubview:_separatorLine];
    
    _targetContentLabel = [[UILabel alloc] init];
    _targetContentLabel.font = FQDRegularFontWithSize(12.0f);
    _targetContentLabel.textColor = RGB(179, 179, 179);
    _targetContentLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_targetContentLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf);
    }];
    
    [_avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.equalTo(weakSelf.contentView).offset(15);
        make.size.equalTo(@(kAvatarImageSize));
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.avatarImageView);
        make.left.equalTo(weakSelf.avatarImageView.mas_right).offset(10);
        make.right.equalTo(weakSelf.contentView).offset(-15);
    }];
    
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.nameLabel);
        make.width.equalTo(weakSelf.nameLabel);
    }];
    
    [_separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.avatarImageView.mas_bottom).offset(10);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.contentView).offset(-15);
        make.height.equalTo(@0.5);
    }];
    
    [_targetContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.separatorLine.mas_bottom).offset(5.5);
        make.left.equalTo(weakSelf.contentView).offset(15);
        make.right.equalTo(weakSelf.contentView).offset(-15);
        make.bottom.equalTo(weakSelf.contentView).offset(-10);
    }];
}

- (void)configureCellWithCellItem:(FQDMyCommentMessageCellItem *)cellItem
{
    [super configureCellWithCellItem:cellItem];
    
    FQDMyCommentMessage *message = cellItem.commentMessage;
    
    UIImage *defaultAvatarImage = [UIImage imageNamed:@"avatar_default_icon_normal"];
    NSURL *avatarURL = [NSURL URLWithString:message.userface];
    [self.avatarImageView sd_setImageWithURL:avatarURL placeholderImage:defaultAvatarImage];
    
    NSString *dateString = [[NSDate dateWithTimeIntervalSince1970:message.publish_time] dateStringSinceNow];
    self.dateLabel.text = [NSString stringWithFormat:@"%@赞了你", dateString];
    
    self.nameLabel.text = message.username;
    self.targetContentLabel.text = [NSString stringWithFormat:@"%@", message.target_content];
    
}

+ (NSString *)reuseId
{
    return @"FQDPraiseOnMeCell";
}

@end
